<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/images/heima.png" alt="" /><span>后台管理界面</span>
      </div>
      <el-button type="info" plain @click="loginOut">退出</el-button></el-header
    >

    <el-container>
      <el-aside :width="isCollapse ? '63px' : '201px'">
        <div class="taggle-button" @click="hanldCollapse">|||</div>

        <el-menu
          background-color="#fff"
          text-color="#404040"
          active-text-color="#1a73e8"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="'/' + activePath"
        >
          <!-- 一级导航 -->
          <el-submenu
            :index="items.id + ''"
            v-for="items in menuList"
            :key="items.id"
          >
            <template slot="title">
              <i :class="iconObj[items.id]"></i>
              <span>{{ items.authName }}</span>
            </template>

            <!-- 二级级导航 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in items.children"
              :key="subitem.id"
              @click="saveRouter(subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span> {{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 主体内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      name: "home",
      isCollapse: false,
      menuList: [],
      iconObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      activePath: "",
    };
  },
  created() {
    this.getMeunList();
    this.activePath = window.sessionStorage.getItem("activepath");
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    hanldCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    async getMeunList() {
      const { data: res } = await this.$http.get("/menus");
      //console.log(res.data);
      if (res.meta.status !== 200)
        return this.$message.error("左侧导航数据获取失败！");

      this.menuList = res.data;
      //console.log(this.menuList);
    },
    saveRouter(activepath) {
      window.sessionStorage.setItem("activepath", activepath);
      this.activePath = activepath;
      //console.log(activepath);
    },
  },
};
</script>
<style lang="less" scoped>
.el-menu--collapse {
  width: 62px !important;
}
.taggle-button {
  line-height: 30px;
  color: #000;
  text-align: center;
  font-size: 12px;
  border-bottom: 1px solid #fafafa;
}
.iconfont {
  margin-right: 10px;
}
.home-container {
  height: 100%;
}
.el-header {
  background: #1a73e8;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 10%);
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  padding-left: 10px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
    }
    span {
      padding-left: 10px;
    }
  }
}
.el-aside {
  background: #fff;
  box-shadow: 2px 0px 10px rgba(0, 0, 0, 10%);
  border-right: #e6e6e6 1px solid;
  position: relative;
}
.el-main {
  background: #fafafa;
}
.el-menu {
  border-right: 0px;
}
.el-scrollbar {
  width: 100%;
  overflow: hidden;
  height: 100%;
}
/deep/.el-scrollbar__wrap {
  overflow-x: hidden;
}

/deep/ .el-scrollbar__view {
  overflow-x: hidden;
}
</style>
